package org.gl.training.gwt.client.view;

import org.gl.training.gwt.client.SessionManager;
import org.gl.training.gwt.client.service.UserService;
import org.gl.training.gwt.client.service.UserServiceAsync;
import org.gl.training.gwt.client.utils.MyAsyncCallback;
import org.gl.training.gwt.shared.model.UserVO;

import com.google.gwt.core.client.GWT;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.FormErrorOrientation;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class LoginView extends Window {

	private UserServiceAsync userService = GWT.create(UserService.class);

	private TextItem userNameItem;

	private PasswordItem passwordItem;

	private DynamicForm form;

	public LoginView() {
		super();
		setMembersMargin(15);
		setWidth(250);
		setHeight(140);

		setTitle("Login");
		setShowMinimizeButton(false);
		setShowCloseButton(false);
		setIsModal(true);
		//setShowModalMask(true);
		centerInPage();

		VLayout layout = new VLayout(10);

		form = new DynamicForm();
		form.setErrorOrientation(FormErrorOrientation.RIGHT);
		form.setNumCols(3);
		form.setColWidths("54%", "5%", "41%");
		form.setLayoutAlign(Alignment.LEFT);

		userNameItem = new TextItem();
		userNameItem.setTitle("User Name");
		userNameItem.setRequired(true);

		passwordItem = new PasswordItem();
		passwordItem.setTitle("Password");
		passwordItem.setType("password");
		passwordItem.setRequired(true);

		form.setFields(new FormItem[] { userNameItem, passwordItem });

		HLayout hLayout = new HLayout();
		hLayout.setMembersMargin(10);

		Button loginButton = new Button("Login");
		loginButton.addClickHandler(new LoginHandler());
		Button registerButton = new Button("Register");
		registerButton.addClickHandler(new RegisterHandler());
		hLayout.addMember(loginButton);
		hLayout.addMember(registerButton);
		hLayout.setAlign(Alignment.CENTER);

		layout.addMember(form);
		layout.addMember(hLayout);

		addItem(layout);
	}

	/**
	 * Click handler that is used when we click Login button
	 * 
	 * @author helen.kravchenko
	 */
	private class LoginHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			if (form.validate()) {
				String username = userNameItem.getValueAsString();
				userService.getUser(username, new LoginCallback());
			}
		}
	}

	private class LoginCallback extends MyAsyncCallback<UserVO> {

		@Override
		public void onSuccess(UserVO userVO) {
			String password = passwordItem.getValueAsString();

			if (userVO == null) {
				showErrorMessage("User is not registered");
			} else if (!userVO.getPassword().equals(password)) {
				showErrorMessage("User password is not correct");
			} else if (!userVO.getUserStatus().equals("Approved")) {
				showErrorMessage("User is waiting for approve");
			} else {
				SessionManager.setCurrentUserVO(userVO);
				hide();
				redirectToPage(userVO);
			}
		}

		private void redirectToPage(UserVO userVO) {
			if (userVO.isAdmin()) {
				AdminView adminView = new AdminView();
				adminView.draw();
			} else {
				DailyPlanView dailyplanView = new DailyPlanView(userVO.getId());
				dailyplanView.draw(); 
			}
		}
	}

	/**
	 * Click handler that is used when we click Register button
	 * 
	 * @author helen.kravchenko
	 */
	private class RegisterHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			hide();

			RegisterView registerView = new RegisterView();
			registerView.show();
		}
	}
}
